package com.taomeng.dao.impl;

import com.taomeng.dao.IPermissionDao;
import com.taomeng.entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class IPermissionDaoImpl implements IPermissionDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<Permission> findUserPermissionsByLoginUserId(Long id) {
        String sql = "select   " +
                " t_permission.id,  " +
                " t_permission.name,  " +
                " t_permission.resource,  " +
                " t_permission.state,  " +
                " t_permission.menu_id,  " +
                " t_permission.sn,  " +
                " t_permission.pid,  " +
                " t_permission.is_parent  " +
                "from  " +
                " t_user,  " +
                " t_user_role,  " +
                " t_role,  " +
                " t_role_permission,  " +
                " t_permission  " +
                "where  " +
                " t_user.id=t_user_role.user_id  " +
                " and  " +
                " t_role.id = t_role_permission.role_id  " +
                " and  " +
                " t_role_permission.permission_id=t_permission.id  " +
                " and   " +
                " t_user.id=?";

        Object[] params = {
                id
        };

        try {
            return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(Permission.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Permission> getAll() {
        String sql = "select * from t_permission";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Permission.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Permission> findPermissionsByRoleId(Long id) {
        String sql = "select\n" +
                "\tt_permission.id,  \n" +
                "\tt_permission.name,    \n" +
                "\tt_permission.resource,   \n" +
                "\tt_permission.state,    \n" +
                "\tt_permission.menu_id,  \n" +
                "\tt_permission.sn,  \n" +
                "\tt_permission.pid,   \n" +
                "\tt_permission.is_parent \n" +
                "from\n" +
                "t_role,\n" +
                "t_role_permission,\n" +
                "t_permission\n" +
                "where\n" +
                "t_role.id = t_role_permission.role_id\n" +
                "and\n" +
                "t_permission.id=t_role_permission.permission_id\n" +
                "and\n" +
                "t_role.id=?";
        Object[] params = {
                id
        };
        try {
            return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(Permission.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
